using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.DOWNLOAD.Controls
{
    public partial class Detail : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hlSignIn.NavigateUrl = "~/SignIn.aspx?returnUrl=" + Server.UrlEncode(Request.RawUrl);
                if (Context.User.Identity.IsAuthenticated)
                {
                    AjaxPanel1.Visible = true;
                    pnlSignIn.Visible = false;
                }
                else
                {
                    AjaxPanel1.Visible = false;
                    pnlSignIn.Visible = true;
                    
                }

                if (this.ItemID > 0)
                {
                    int itemID = Convert.ToInt32(Request.QueryString["itemID"]);
                    Components.Item item = Components.Item.Load(itemID);                    
                    if (item != null)
                    {
                        lblTitle.Text = item.Title;
                        lblCreationTime.Text = item.CreatedDate.ToString();
                        lblCreatedBy.Text = item.CreatedBy;
                        lblTotalDownloads.Text = item.TotalDownloads.ToString("N0");
                        lblFileSize.Text = FileHelper.GetFileSizeString(item.FileSize);
                        imgImageUrl.ImageUrl = item.ImageUrl;
                        imgImageUrl.Visible = item.ImageUrl.Trim().Length > 0;
                        lblDescription.Text = Server.HtmlDecode(item.Description);

                        string[] urls = item.Url.Split('|');
                        hlDownload.NavigateUrl = urls[0];
                        if (urls.Length > 1) hlDownload01.NavigateUrl = urls[1];
                        if (urls.Length > 2) hlDownload02.NavigateUrl = urls[2];
                        if (urls.Length > 3) hlDownload03.NavigateUrl = urls[3];

                        Random rnd = new Random();
                        int digit1 = rnd.Next(0, 9);
                        imgNumber1.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit1);

                        int digit2 = rnd.Next(0, 9);
                        imgNumber2.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit2);

                        int digit3 = rnd.Next(0, 9);
                        imgNumber3.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit3);

                        int digit4 = rnd.Next(0, 9);
                        imgNumber4.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit4);

                        ViewState["Digit"] = digit1 + "" + digit2 + "" + digit3 + "" + digit4;
                    }
                }
            }
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            if (ViewState["Digit"] != null)
            {
                tblDownloads.Visible = txtDigit.Text == ViewState["Digit"].ToString();
                hlDownload.Visible = hlDownload.NavigateUrl.Length > 0;
                hlDownload01.Visible = hlDownload01.NavigateUrl.Length > 0;
                hlDownload02.Visible = hlDownload02.NavigateUrl.Length > 0;
                hlDownload03.Visible = hlDownload03.NavigateUrl.Length > 0;

                if (txtDigit.Text == ViewState["Digit"].ToString())
                {                    
                    Components.Item.IncreaseTotalDownloads(this.ItemID);
                    tblCheck.Visible = false;
                }
            }
        }
    }
}